package com.woniuxy.orderassembly.controller;


import com.woniuxy.api.OrderApi;
import com.woniuxy.common.Result;
import com.woniuxy.form.OrderForm;
import com.woniuxy.form.OrderTransferForm;
import com.woniuxy.form.QueryOrderForm;

import com.woniuxy.orderassembly.model.Order;
import com.woniuxy.orderassembly.service.IOrderService;
import com.woniuxy.orderassembly.util.Publisher.IMQAddOrder;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chestnut
 * @since 2021-05-24
 */
@RestController
@Slf4j
public class OrderController implements OrderApi {

    @Resource
    private IMQAddOrder imqAddOrder;

    @Resource
    private IOrderService orderService;

    /**
     * 用户下单后调用平台生成订单
     * 生成订单后 发送信息至酒店
     * @param orderForm
     * @return
     */
    //新增订单
    @Override
    public Result userPlaceOrder(@RequestBody OrderForm orderForm) {
        log.info("下单信息：{}",orderForm);
        Result order = orderService.createOrder(orderForm);
        return Result.success(order);
    }



//    @Override
//    public Result addOrder(OrderForm orderForm) {
//        return null;
//    }


    /**
     * 动态查询订单
     * 酒店端条件：酒店id全查询，酒店id和状态查询订单（已完成的订单，未完成的订单）分页展示
     * @param hotelId
     * @param status
     * @param pageIdx
     * @return
     */
    @Override
    public Result hotelQueryOrder(@RequestParam(value = "hotelId")Integer hotelId,
                                  @RequestParam(value = "status")Integer status,
                                  @RequestParam(value = "pageIdx",required = false,defaultValue="5")Integer pageIdx) {
        log.info("酒店分页查询条件：酒店id:{} 订单状态:{} 页下标:{}",hotelId,status,pageIdx);
        Result order = orderService.hotelQueryOrderPage(hotelId,status,pageIdx);
        return Result.success(order);
    }

    @Override
    public Result userQueryOrder(@RequestParam(value = "userId")Integer userId,
                                 @RequestParam(value = "status")Integer status,
                                 @RequestParam(value = "pageIdx",required = false,defaultValue="5")Integer pageIdx) {
        log.info("用户分页查询条件：用户id:{} 订单状态:{} 页下标:{}",userId,status,pageIdx);
        Result order = orderService.userQueryOrderPage(userId,status,pageIdx);
        return Result.success(order);
    }

    /**
     * 平台流水分页查询
     * 酒店端条件：酒店id全查询，酒店id和状态查询订单（已完成的订单，未完成的订单）分页展示
     * @param pageIdx
     * @return
     */
    @Override
    public Result lookOnedayWater(@RequestParam(value = "pageIdx",required = false,defaultValue="10")Integer pageIdx) {
        return null;
    }

    /**
     * 商家对订单的处理，接受发送信息至用户
     * 对订单处理为修改状态
     * @param hotelId
     * @return
     */
    @Override
    public Result hotelManageOrder(@RequestParam(value = "hotelId")Integer hotelId,@RequestParam(value = "bool")Boolean bool) {
        log.info("酒店处理订单,{}",hotelId);
        Result result = orderService.hotelManageOrder(hotelId,bool);
        return result;
    }

    @Override
    public Result payMoney() {
        return null;
    }


    //查询所有已完成的订单
    @Override
    public List<OrderTransferForm> findOrderByStatus() {

        List<OrderTransferForm> orders = orderService.findOrderByStatus();

        log.info("已完成订单的id 和金额{}",orders);
        return orders;
    }


    //根据订单id修改订单状态
    @Override
    public Result modifyOrderStatus(Integer status,Integer orderId) {
        orderService.modifyOrderStatus(status,orderId);
        log.info("要修改的订单id为{},修改状态为{}",orderId,status);
        return Result.success();
    }

    @Override
    public Result send(String routingKing) {
        Order order = new Order();
        order.setOrderId(321);
        Result result = null;
        try {
            result = imqAddOrder.addRoder(order, routingKing);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.success("出了一些小问题");
        } catch (TimeoutException e) {
            e.printStackTrace();
            return Result.success("出了一些小问题");
        }
        return result;
    }


}

